/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.beans.jpa;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import net.easoftware.code.beans.Mes;
import net.easoftware.code.beans.facade.CargaDatosNav;
import net.easoftware.code.beans.maintenance.navigator.MbNavigator;
import net.easoftware.code.dao.CargasDao;
import net.easoftware.code.dao.CatalogosDao;
import net.easoftware.code.error.EaError;
import net.easoftware.code.util.Tools;

/**
 *
 * @author Eliu
 */
@SessionScoped
@Named
public class MbCargasDatos implements Serializable {

    @Inject
    CatalogosDao dao;
    @Inject
    CargasDao cargasDao;
    @Inject
    MbNavigator mbNavigator;
    private CargaDatosNav selected;
    private List<CargaDatosNav> cargas = new LinkedList<>();
    private boolean showNavigator = false;
    private Map<Integer, Mes> meses;

    public void recargar() {
        cargas = new LinkedList<>();
    }

    public Map<Integer, Mes> getMeses() {
        if (meses == null || meses.isEmpty()) {
            meses = Tools.getMesesMap();
        }
        return meses;
    }

    public void setMeses(Map<Integer, Mes> meses) {
        this.meses = meses;
    }

    public boolean isShowNavigator() {
        return showNavigator;
    }

    public void setShowNavigator(boolean showNavigator) {
        this.showNavigator = showNavigator;
    }

    public CargaDatosNav getSelected() {
        return selected;
    }

    public void setSelected(CargaDatosNav selected) {
        this.selected = selected;
    }

    public List<CargaDatosNav> getCargas() {
        if (this.cargas.isEmpty()) {
            try {
                List<CargaDatosNav> l = dao.getAllCargasDatos();
                this.cargas = new LinkedList<>();
                for (CargaDatosNav c : l) {
                    c.setAsoff(c.getIsasoff() != null && c.getIsasoff().intValue() > 0);
                    c.setChequesRechazados(c.getIschequesrechazados() != null && c.getIschequesrechazados().intValue() > 0);
                    c.setOrdenes(c.getIsordenes() != null && c.getIsordenes().intValue() > 0);
                    c.setOrdenesRetenidas(c.getIsordenesretenidas() != null && c.getIsordenesretenidas().intValue() > 0);
                    c.setVentas(c.getIsventas() != null && c.getIsventas().intValue() > 0);
                    c.setTotalFilas(c.getIsasoff() + c.getIschequesrechazados() + c.getIsordenes() + c.getIsordenesretenidas() + c.getIsventas());
                    this.cargas.add(c);
                }
            }
            catch (EaError ex) {
                Logger.getLogger(MbCargasDatos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return cargas;
    }

    public void setCargas(List<CargaDatosNav> cargas) {
        this.cargas = cargas;
    }

    public void actionSelect() {
        mbNavigator.setNavigator(selected.getNavConfig());
        mbNavigator.loadNavigator();
        setShowNavigator(true);
    }

    public void doDelete() {
        cargasDao.eliminarCarga(selected.getId());
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Datos Eliminados", "La informacion de la carga de dato se ha eliminado por completo!"));
        cargas.clear();
    }
}
